var globalList = new Array();
var globalValue = new Array();
var globalPage = 1; 
$(document).ready(function() {
    getContact(globalPage);
    $("#edit-name").change(function() {
        console.log(1);
        getContact(1);
    });
    $("#edit-email").change(function() {
        console.log(1);
        getContact(1);
    });
    $("#next_link").click(function() {
        globalPage++;
        getContact(globalPage);
    });
    $("#pre_link").click(function() {
        globalPage--;
        getContact(globalPage);
    });
    $('.checkall').click(function () {
            $jq(this).closest('table').find('input[type=checkbox]:visible').prop('checked', this.checked);
        });
    $("#contactListForm input[class=form-checkbox]").click(function () {
            //alert("d");
        });
    $("table").click(function(event) {
    $("#log").html("clicked: " + event.target.nodeName);
    if(event.target.nodeName === 'INPUT'){
        removeSavedContact(event.target);
    }
    
    });
    $("#spinner").bind("ajaxSend", function() {
        $(this).css("visibility", "visible");
    }).bind("ajaxStop", function() {
        $(this).css("visibility", "hidden");
    }).bind("ajaxError", function() {
        $(this).css("visibility", "hidden");
    });

});

function getContact(page) {
    var pageUrl = 'http://' + document.domain;
    
    var name = $("#edit-name").val();
    var email = $("#edit-email").val();
    var checked = '';
    var extraLink = '';
    //var page = 1;
    var size = 20;
    //var count = 0;
    var pagesize = page * size;
    //var mycount = page * size;
    if (name == null || name == ''){
        name = '%2A';
    }
    if (email == null || email == ''){
        email = '%2A';
    }
    var savedList = saveConact();
    //console.log(savedList);
    if(savedList.length > 0){
        for (var i=0;i<savedList.length;i++){
            
            var contactId = savedList[i].getAttribute('id');
            var contactValue = savedList[i].getAttribute('value');
            //console.log(contactValue);
            if ($.inArray(contactId, globalList) === -1 && contactId != null){
                globalList.push(contactId);
                globalValue.push(contactValue);
            }
        }
        //console.log(globalList);
    }
    if(pageUrl == 'http://localhost'){
        extraLink = '/fundraisingmagsdev';
    }
    else{
        extraLink = '';
    }
    $.getJSON(pageUrl + extraLink + '/contacts_management/email/' + name + '/' + email + '/' + page + '/' + size, function(json) {
        $("table").find("tr:gt(0)").hide();
        if(page == 1)
        $("#pre_link").css("visibility", "hidden");
        else
            $("#pre_link").css("visibility", "visible");
        if(json['total'] < json['page'])
            $("#next_link").css("visibility", "hidden");
            else
            $("#next_link").css("visibility", "visible");
        var count = 0;
        $.each(json, function(index, array) {
            var start = (page - 1) * size;
            
            if (count >= start && pagesize > count && array != null)
            {
            $("table").find("#" + array['nid']).show();
            
            }
            
            count++;
            
        });
    });
     
}
function saveConact(){
    var allchecked = $("table :checked");
    return allchecked;
}
function removeSavedContact(contact){
    var clickedContact;
    var clickedContactValue;
    clickedContact = contact.getAttribute('id');
    clickedContactValue = contact.getAttribute('value');
    if($.inArray(clickedContact, globalList) !== -1){
        var contactIndex = globalList.indexOf(clickedContact);
        var valueIndex = globalValue.indexOf(clickedContactValue);
        globalList.splice(contactIndex, 1);
        globalValue.splice(valueIndex, 1);
        
    }
        
}
